package android.controllers;

import android.activities.CourseActivity;
import android.activities.GoogleMapsActivity;
import android.activities.R;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.models.Step;
import android.os.Bundle;
import android.tools.ExtendedAlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

public class CourseController implements View.OnClickListener, OnChildClickListener, DialogInterface.OnClickListener
{
	public final static String BTN_SUBMIT = "Valider";
	public final static String LATITUDE = "latitude";
	public final static String LONGITUDE = "longitude";
	public final static int VIEW_MAP = 1;
	
	private CourseActivity _view;
	private Step _step;
	
	public CourseController(CourseActivity a)
	{
		_view = a;
	}
	
	public void onClick(View v)
	{
		Log.i("CourseController", "OnClick - START ");
		
		switch(v.getId())
		{
			case VIEW_MAP :
				Toast.makeText(v.getContext(), "view map", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(_view, GoogleMapsActivity.class);
				Bundle bundle = new Bundle();
				
				bundle.putInt(LATITUDE, _step.getGeoPoint().getLatitudeE6());
				bundle.putInt(LONGITUDE, _step.getGeoPoint().getLongitudeE6());
				
				_view.startActivity(intent.putExtras(bundle));
				break;
			case 2 :
				
				break;
		}
		
		Log.i("CourseController", "OnClick - END ");
	}

	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
	{
		_step = new Step();

		ExtendedAlertDialog dialog = new ExtendedAlertDialog(1, v.getContext());
		
		dialog.setTitle(_step.getName());
        View view = ((LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.step, null);
		
        EditText txtAnswer = (EditText)view.findViewById(R.id.step_txtAnswer);
		txtAnswer.setText(_step.getAnswer());
		Button btnViewMap = (Button)view.findViewById(R.id.step_btnViewMap);
		btnViewMap.setOnClickListener(this);
		btnViewMap.setId(VIEW_MAP);
		
		dialog.setView(view);
		dialog.show();
		
		return true;
	}

	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		
	}
}
